

#import "UITableView+Utils.h"

@implementation UITableView (Utils)

- (void)registerCellByNibName:(NSString*)nibName {
    [self registerNib:[UINib nibWithNibName:nibName bundle:nil] forCellReuseIdentifier:nibName];
}

- (void)registerCellByClassName:(NSString*)nameClass{
    [self registerClass:NSClassFromString(nameClass) forCellReuseIdentifier:nameClass];
}

/// 结束上下拉刷新
- (void)endRefresh {
    [self.mj_header endRefreshing];
    [self.mj_footer endRefreshing];
}

#pragma mark - 显示暂无数据

/// 显示无数据tipView
- (void)showNoData {
    [self showNoDataWithimageName:nil tipText:nil];
}

- (void)showNoDataWithTipText:(NSString *)tipText {
    [self showNoDataWithimageName:nil tipText:tipText];
}

/// 显示不固定的图片
- (void)showNoDataWithimageName:(NSString*)name tipText:(NSString *)tipText{
    [self hideNoData];
    
    NSString *imageName = name == nil ? @"" : name;
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.tag = 1234;
    imgView.contentMode = UIViewContentModeCenter;
    UIImage *image = [UIImage imageNamed:imageName];
    imgView.image = image;
    CGSize imageSize = image.size;
    CGFloat scale = LLScreenWidth < 375 ? 0.7 : 0.9;
    imgView.frame = CGRectMake((self.width - imageSize.width * scale)/2.0, (self.height - imageSize.height * scale - 64)/2.0, imageSize.width * scale, imageSize.height * scale);
    [self addSubview:imgView];
    [self bringSubviewToFront:imgView];
    
    if (tipText && tipText.length) {
        UILabel *lable = [[UILabel alloc] init];
        lable.text = tipText;
        lable.tag = 12345;
        lable.frame = CGRectMake(30, self.height / 2, self.width - 60, 30);
        lable.textAlignment = NSTextAlignmentCenter;
        lable.textColor = LLColorRGB(198);
        lable.font = LLRegularFont(16);
        [self addSubview:lable];
        [self bringSubviewToFront:lable];
    }
}

/// 隐藏无数据tipView
- (void)hideNoData {
    if ([self viewWithTag:1234]) {
        [[self viewWithTag:1234] removeFromSuperview];
    }
    if ([self viewWithTag:12345]) {
        [[self viewWithTag:12345] removeFromSuperview];
    }
}


@end
